<template>
  <BasicModal
    v-bind="$attrs"
    :title="t('routes.demo.documentWorkPlace.documentHandover')"
    @register="register"
    @ok="handleSubmit"
    :okText="t('routes.demo.documentManager.handover')"
    :destroyOnClose="true"
    :minHeight="250"
    :width="800"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getDocAssignModal } from './tableData';
  import { workAssign } from '/@/api/demo/document';

  const { t } = useI18n();
  const { createMessage } = useMessage();
  const emit = defineEmits(['register', 'success']);
  const workList = ref<any>([]);
  const holder = ref<string>('');

  // 提交
  async function handleSubmit() {
    if (holder.value === getFieldsValue().receiver) {
      createMessage.error(t('routes.demo.documentManager.cannotTransferToSelf'));
    } else {
      workAssign(getFieldsValue().receiver, workList.value).then(() => {
        createMessage.success(t('routes.demo.documentManager.transferSuccess'));
        closeModal();
        emit('success');
      });
    }
  }

  // 初始化弹窗
  const [register, { setModalProps, closeModal }] = useModalInner((data) => {
    resetFields();
    setModalProps({ confirmLoading: false });
    setFieldsValue({ ...data.workAssignList });
    setFieldsValue({ ...data.recordData });
    workList.value = data.workAssignList.fileList;
    holder.value = data.workAssignList.holder;
  });

  // 初始化表单
  const [registerForm, { resetFields, setFieldsValue, getFieldsValue }] = useForm({
    labelCol: { span: 6 },
    wrapperCol: { span: 16 },
    schemas: getDocAssignModal(),
    showResetButton: false,
    showSubmitButton: false,
  });
</script>
